import '../base/base_import.dart';

typedef AppNavCall = Future Function(Object data);

class APPNavigator {

  /*
  普通添加
   */
  static  pushNamed(BuildContext context, String routeId, {params, obj, routeArgument, AppNavCall popCallback}){
    if(routeArgument == null) {
      routeArgument = new RouteArguments();
      routeArgument.params = params;
      routeArgument.obj = obj;
    }
    Navigator.pushNamed(context, routeId, arguments: routeArgument).then((data){
      if(popCallback != null)
        popCallback(data);
    });
  }

  /*
  替换，旧页面的弹出，新页面的进入
   */
  static pushReplacement(BuildContext context, String routeId, {params, routeArgument}){
    if(routeArgument == null) {
      routeArgument = new RouteArguments();
      routeArgument.params = params;
    }
    Navigator.popAndPushNamed(context, routeId, arguments: routeArgument);
  }

  /*
  替换，仅显示新页面的进入
   */
  static popReplacement(BuildContext context, String routeId, {params, routeArgument}){
    if(routeArgument == null) {
      routeArgument = new RouteArguments();
      routeArgument.params = params;
    }
    Navigator.pushReplacementNamed(context, routeId, arguments: routeArgument);
  }

  /*
  删除到指定页面，(Routeroute) => false 能够确保删除先前所有实例
   */
  static pushNamedAndRemoveUntil(BuildContext context, String routeId, {params, routeArgument}){
    if(routeArgument == null) {
      routeArgument = new RouteArguments();
      routeArgument.params = params;
    }
    Navigator.of(context).pushNamedAndRemoveUntil(routeId,(Route<dynamic> route) => false, arguments: routeArgument);
  }

  /*
  添加并删除部分页面
   */
  static pushNamedAndRemoveUntilName(BuildContext context, String newRouteId, String oldRouteId,{params, routeArgument}){
    if(routeArgument == null) {
      routeArgument = new RouteArguments();
      routeArgument.params = params;
    }
    Navigator.of(context).pushNamedAndRemoveUntil(newRouteId,ModalRoute.withName(oldRouteId/*'/oldRouteId'*/), arguments: routeArgument);
  }

  /*
  普通回退
   */
  static  pop(BuildContext context, { Object data }){
    Navigator.pop(context, data);
  }

  /*
  回退到指定页面
   */
  static popUtil(BuildContext context, String routeId ){
    Navigator.popUntil(context, ModalRoute.withName(routeId));
  }
}

class RouteArguments {
  Map<String, dynamic> params;//普通map传参
  dynamic obj;//传输对象
}